﻿using System;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Web;
using LibGlobal;
using Roles;

namespace ERP.Page
{
    public partial class Accounting : System.Web.UI.Page
    {
        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermisstion();
            if (Session["login"] == null && Session["UserName"] == null)
            {
                Response.Redirect(@"../Login.aspx");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
                Response.Cache.SetNoStore();
            }
            else
            {
                lbServerNameTitile.Text = Session["UserName"].ToString();
                //Get MachineName
                lbMachineName.Text = Environment.MachineName;
                //Get Ip Address
                var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (var networkInterface in networkInterfaces)
                {
                    lbIdAdress.Text =
                        GetLocalIPv4(networkInterface.NetworkInterfaceType != NetworkInterfaceType.Wireless80211
                            ? NetworkInterfaceType.Ethernet
                            : NetworkInterfaceType.Wireless80211);
                }
                //Get Session Status
                var o1 = Session["login"];
                if (o1 != null) lbSessionStatus.Text = o1.ToString();
            }
        }
        #endregion

        #region[Get-Ip-Local]
        /*
         * GetLocalIPv4(NetworkInterfaceType.Ethernet);Lấy địa chỉ IPv4 Cáp mạng
         * GetLocalIPv4(NetworkInterfaceType.Wireless80211);Lấy địa chỉ IPv4 Wifi
         */
        public string GetLocalIPv4(NetworkInterfaceType _type)
        {
            string output = "";
            foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
                {
                    foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            output = ip.Address.ToString();
                        }
                    }
                }
            }
            return output;
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                #region[Modules-Main]
                var Is_PageKeToan = SecurityServices.HasPermisstionPageMain(StringContent.KeToan, int.Parse(Session["USer_Detail_Id"].ToString()), true);
                {
                    if (Is_PageKeToan == false)
                    {
                        PanelKeToanModule.Visible = false;
                    }
                }
                #endregion

                #region[Modules-Parrent]
                var Is_PageIncome_Expenditure_Method = SecurityServices.HasPermisstionPageParrent(StringContent.Income_Expenditure_Method, int.Parse(Session["USer_Detail_Id"].ToString()), true);
                {
                    if (Is_PageIncome_Expenditure_Method == false)
                    {
                        ShowTabKhoanMucThuChi.Visible = false;
                    }
                }
                var Is_PageBank_Account = SecurityServices.HasPermisstionPageParrent(StringContent.Bank_Account, int.Parse(Session["USer_Detail_Id"].ToString()), true);
                {
                    if (Is_PageBank_Account == false)
                    {
                        ShowTabTaiKhoanThuChi.Visible = false;
                    }
                }
                var Is_PagePhanHeKeToan = SecurityServices.HasPermisstionPageParrent(StringContent.AssignedAccountLeve, int.Parse(Session["USer_Detail_Id"].ToString()), true);
                {
                    if (Is_PagePhanHeKeToan == false)
                    {
                        ShowTabPhanHeKeToan1.Visible = false;
                    }
                }
                var Is_PagePhanHeKeToan2 = SecurityServices.HasPermisstionPageParrent(StringContent.AssignedAccountLeve2, int.Parse(Session["USer_Detail_Id"].ToString()), true);
                {
                    if (Is_PagePhanHeKeToan2 == false)
                    {
                        ShowTabPhanHeKeToan2.Visible = false;
                    }
                }
                #endregion
            }
        }
        #endregion
    }
}